package com.cskaoyan.realm;

import com.cskaoyan.domain.MarketUser;
import com.cskaoyan.service.AuthService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserRealm extends AuthorizingRealm {
    @Autowired
    AuthService authLoginService;
    //用户角色的权限 授权使用
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    //用户的角色.认证使用
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //登录输入的账号密码
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        //数据库的
        MarketUser marketUser = authLoginService.queryUserAndName(token.getUsername());
        //存在用户名
        if(marketUser!=null){
            return new SimpleAuthenticationInfo(marketUser,marketUser.getPassword(),getName());
        }


        return null;

    }
}
